<template>
<div class="login-container">
        <el-form :model="ruleForm2" :rules="rules2"
         status-icon
         ref="ruleForm2" 
         label-position="left" 
         label-width="0px" 
         class="demo-ruleForm login-page">
            <h3 class="title">系统登录</h3>
            <el-form-item prop="username">
                <el-input type="text" 
                    v-model="ruleForm2.username" 
                    auto-complete="off" 
                    placeholder="用户名"
                ></el-input>
            </el-form-item>
                <el-form-item prop="password">
                    <el-input type="password" 
                        v-model="ruleForm2.password" 
                        auto-complete="off" 
                        placeholder="密码"
                    ></el-input>
                </el-form-item>
            <el-checkbox 
                v-model="checked"
                class="rememberme"
            >记住密码</el-checkbox>
            <el-form-item style="width:100%;">
                <el-button type="primary" style="width:100%;"  :loading="logining">登录</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>
<script setup>
import { ref,onBeforeMount, reactive } from 'vue'
import { useRoute,useRouter } from 'vue-router';
import { login } from "@/api/login";
import { useCommonStore } from "@/stores";
const router = useRouter()
const route = useRoute()
const commonStore = useCommonStore()
const checked = ref(false)
const rules2 = reactive({
  userName: [
    { required: true, message: '请输入账户', trigger: 'blur' },
  ],
  passWord: [
    { required: true, message: '请输入密码', trigger: 'blur'}
  ]
})

const logining = ref(false)
const ruleForm2 = ref({ userName: '',passWord: '' })

onBeforeMount(async ()=>{
  const params = route.params
  const query = route.query
  console.log(params,query)
  if(params.tk==='getToken'){//跳转登录页面

  }else{//自动登录
    const token = commonStore.getToken;
    if(token){
      if(query.isFull){
        localStorage.setItem('isFull',1)
      }else{
        localStorage.removeItem('isFull')
      }
      if(query.name){
        return router.replace({name:query.name})
      }else{
        return router.replace('/')
      }
    }
    const res =  await login({tk:params.tk})
    console.log(res);
    if(res.code===1000){
      localStorage.setItem('token',res.data.token)
      localStorage.setItem('userInfo',JSON.stringify(res.data))
      commonStore.token = res.data.token
      if(query.isFull){
        localStorage.setItem('isFull',1)
      }else{
        localStorage.removeItem('isFull')
      }
      if(query.name){
        router.replace({name:query.name})
      }else{
        router.replace('/')
      }
    }
  }
})
</script>
<style lang="less" scoped>
.login-container {
    width: 100%;
    height: 100vh;

    background: #4373a5;
 
    /* 登录框上下对齐 */
    display: flex;
    align-items: center;
}
.login-page {
    -webkit-border-radius: 5px;
    border-radius: 5px;
    margin: 180px auto;
    width: 350px;
    padding: 35px 35px 15px;
    background: #fff;
    border: 1px solid #eaeaea;
    // box-shadow: 0 0 25px #cac6c6;
}
label.el-checkbox.rememberme {
    margin: 0px 0px 15px;
    text-align: left;
}
.title{
  font-weight: bold;
  margin-bottom: 2vh;
}
.el-container .el-main{    
    --el-main-padding: 20px;
    display: block;
    flex: 1;
    flex-basis: auto;
    overflow: auto;
    box-sizing: border-box;
    // padding: var(--el-main-padding);
}

</style>